package com.frxuexi.park2302.notice

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.paging.PagingData
import androidx.recyclerview.widget.LinearLayoutManager
import com.frxuexi.lib_base.entity.NoticeBeanItem
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.park2302.R
import com.frxuexi.park2302.databinding.ActivityNoticeBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
class NoticeActivity :BaseActivity<ActivityNoticeBinding,NoticeIntent,NoticeViewModel>() {
    override val binding by lazy { ActivityNoticeBinding.inflate(layoutInflater) }
    override val vm by viewModels<NoticeViewModel>()
    val noticeAdapter by lazy { NoticeAdapter() }


    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        Log.i("lr",it.data.toString())
                        val items = it.data as Collection<NoticeBeanItem>
                        noticeAdapter.addData(items.filter { !it.title.isNullOrEmpty() && !it.content.isNullOrEmpty()})
                        noticeAdapter.notifyDataSetChanged()
                    }
                    is UiState.onError->{
                        Log.i("lr",it.msg)
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        binding.rvNotice.apply {
            adapter=noticeAdapter
            layoutManager=LinearLayoutManager(this@NoticeActivity)
        }

        vm.sendIntent(NoticeIntent.getNotice(1,10))

    }


}